import { connectCar, getCarInfo } from '@/api'
import { connectWebSocket, tryCloseWS } from '@/composables/useUpperControl'
import { jumpToCoordinate, map } from '@/shared/map/base'
import { useRecordStore } from '@/shared/map/record'
import { map1 } from '@/shared/map/showBase'
import { useCarStore } from '@/stores/car'
import { useConfigCameraStore } from '@/stores/configCamera'
import { useSwitchComponents } from '@/stores/switchComponents'
import { Map } from 'maptalks'
import * as carMarker from './carMarker'
import { initMarker } from './carMarker'
import * as carStatus from './carStatus'

const jumpToCar = async (data: any, map: Map) => {
  const x = data.longitude
  const y = data.latitude
  jumpToCoordinate(x, y, map)
}

// const getCameraList = async (code: string | number) => {
//   const res = await getCameraListByCode(code, 'patroling')
//   cameraList.value = res.data
// }

const handleCarStatus = () => {
  carStatus.tryCloseWS()
  carStatus.connectWebSocket()
  tryCloseWS()
  connectWebSocket()
}

const handleCarPoisition = (data: any) => {
  carMarker.addMarker(data)
  carMarker.onCarPoisition()
}

const clearRecordPathPoints = () => {
  const record = useRecordStore()
  record.recordPathPoints.length = 0
}

export default async (code: string | number) => {
  const configCameraStore = useConfigCameraStore()
  const switchComponents = useSwitchComponents()
  const { data } = await getCarInfo(code)
  const carStore = useCarStore()
  carStore.carCustomMode = data.customMode
  carStore.carRainfallNum = data.rainfallNum
  jumpToCar(data, map)
  jumpToCar(data, map1)
  // getCameraList(code)
  //获取摄像头列表
  configCameraStore.getList()
  if (switchComponents.showComponent === true) {
    connectCar(code)
    handleCarPoisition(data)
    handleCarStatus()
    clearRecordPathPoints()
  } else {
    initMarker(data)
    carMarker.onCarPoisition()
  }
}
